<template>
  <el-dialog
    :title="!dataForm.tbRemarkId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="账户昵称" prop="tbAccountName">
      <el-input v-model="dataForm.tbAccountName" placeholder="账户昵称"></el-input>
    </el-form-item>
    <el-form-item label="发布时间" prop="tbReleaseTime">
      <el-input v-model="dataForm.tbReleaseTime" placeholder="发布时间"></el-input>
    </el-form-item>
    <el-form-item label="点评内容" prop="tbRemarkContent">
      <el-input v-model="dataForm.tbRemarkContent" placeholder="点评内容"></el-input>
    </el-form-item>
    <el-form-item label="价格评分" prop="tbMoneyGrade">
      <el-input v-model="dataForm.tbMoneyGrade" placeholder="价格评分"></el-input>
    </el-form-item>
    <el-form-item label="配套评分" prop="tbMatingGrade">
      <el-input v-model="dataForm.tbMatingGrade" placeholder="配套评分"></el-input>
    </el-form-item>
    <el-form-item label="环境评分" prop="tbEnvironmentGrade">
      <el-input v-model="dataForm.tbEnvironmentGrade" placeholder="环境评分"></el-input>
    </el-form-item>
    <el-form-item label="地段评分" prop="tbDistrictGrade">
      <el-input v-model="dataForm.tbDistrictGrade" placeholder="地段评分"></el-input>
    </el-form-item>
    <el-form-item label="交通评分" prop="tbTrafficGrade">
      <el-input v-model="dataForm.tbTrafficGrade" placeholder="交通评分"></el-input>
    </el-form-item>
    <el-form-item label="楼盘ID" prop="housesId">
      <el-input v-model="dataForm.housesId" placeholder="楼盘ID"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          tbRemarkId: 0,
          tbAccountName: '',
          tbReleaseTime: '',
          tbRemarkContent: '',
          tbMoneyGrade: '',
          tbMatingGrade: '',
          tbEnvironmentGrade: '',
          tbDistrictGrade: '',
          tbTrafficGrade: '',
          housesId: ''
        },
        dataRule: {
          tbAccountName: [
            { required: true, message: '账户昵称不能为空', trigger: 'blur' }
          ],
          tbReleaseTime: [
            { required: true, message: '发布时间不能为空', trigger: 'blur' }
          ],
          tbRemarkContent: [
            { required: true, message: '点评内容不能为空', trigger: 'blur' }
          ],
          tbMoneyGrade: [
            { required: true, message: '价格评分不能为空', trigger: 'blur' }
          ],
          tbMatingGrade: [
            { required: true, message: '配套评分不能为空', trigger: 'blur' }
          ],
          tbEnvironmentGrade: [
            { required: true, message: '环境评分不能为空', trigger: 'blur' }
          ],
          tbDistrictGrade: [
            { required: true, message: '地段评分不能为空', trigger: 'blur' }
          ],
          tbTrafficGrade: [
            { required: true, message: '交通评分不能为空', trigger: 'blur' }
          ],
          housesId: [
            { required: true, message: '楼盘ID不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.tbRemarkId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.tbRemarkId) {
            this.$http({
              url: this.$http.adornUrl(`/building/remark/info/${this.dataForm.tbRemarkId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tbAccountName = data.remark.tbAccountName
                this.dataForm.tbReleaseTime = data.remark.tbReleaseTime
                this.dataForm.tbRemarkContent = data.remark.tbRemarkContent
                this.dataForm.tbMoneyGrade = data.remark.tbMoneyGrade
                this.dataForm.tbMatingGrade = data.remark.tbMatingGrade
                this.dataForm.tbEnvironmentGrade = data.remark.tbEnvironmentGrade
                this.dataForm.tbDistrictGrade = data.remark.tbDistrictGrade
                this.dataForm.tbTrafficGrade = data.remark.tbTrafficGrade
                this.dataForm.housesId = data.remark.housesId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/building/remark/${!this.dataForm.tbRemarkId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'tbRemarkId': this.dataForm.tbRemarkId || undefined,
                'tbAccountName': this.dataForm.tbAccountName,
                'tbReleaseTime': this.dataForm.tbReleaseTime,
                'tbRemarkContent': this.dataForm.tbRemarkContent,
                'tbMoneyGrade': this.dataForm.tbMoneyGrade,
                'tbMatingGrade': this.dataForm.tbMatingGrade,
                'tbEnvironmentGrade': this.dataForm.tbEnvironmentGrade,
                'tbDistrictGrade': this.dataForm.tbDistrictGrade,
                'tbTrafficGrade': this.dataForm.tbTrafficGrade,
                'housesId': this.dataForm.housesId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
